package com.wingstudio.springcloud.controller;

import com.wingstudio.springcloud.annotation.RedisLock;
import com.wingstudio.springcloud.redis.RedisLockService;
import com.wingstudio.springcloud.redis.RedisService;
import com.wingstudio.springcloud.service.PaymentService;
import com.wingstudio.springcloud.service.SurvivalClamProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Author ITcz
 * @Data 2021-04-15 - 16:36
 */
@RestController
@RequestMapping("/payment/hystrix")
@Slf4j
@Component(value = "paymentController")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Autowired
    RedisService redisService;

    @Autowired
    RedisLockService redisLockService;

    @Value("${server.port}")
    private String serverPort;


    @GetMapping("/redisLock1/{id}")
    @RedisLock(prefix = "payment", key = "#key")
    public String paymentInfo_RedisLock1(@PathVariable("id") Integer key) {
        try {
            synchronized (this){
                Thread.sleep(5000);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "基于redis实现的分布式锁111111执行完毕！";
    }

    @GetMapping("/redisLock2/{id}")
    @RedisLock(prefix = "payment", key = "#key")
    public String paymentInfo_RedisLock2(@PathVariable("id") Integer key) {
        try {
            //this是目标对象，自然无法实现代理类的增强方法。需要先去尝试获取代理类
            PaymentController service = AopContext.currentProxy() != null ? (PaymentController)AopContext.currentProxy() : this;
            System.out.println(service.paymentInfo_RedisLock1(key));
            synchronized (this){
                Thread.sleep(5000);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "基于redis实现的分布式锁22222222执行完毕！";
    }

    @GetMapping("/lock/{id}")
    public String paymentInfo_Lock(@PathVariable("id") Integer id) {
        String requestId = UUID.randomUUID().toString();
        String lockId = id.toString();
        boolean result = redisLockService.lock("paymentInfo" +":" + lockId, requestId);
        log.info("**********result" +result);
//        if (result == true) {
//            SurvivalClamProcessor survivalClamProcessor
//                    = new SurvivalClamProcessor(lockField, lockKey, randomValue, lockTime);
//            Thread survivalThread = new Thread(survivalClamProcessor);
//            survivalThread.setDaemon(Boolean.TRUE);
//            survivalThread.start();
//            Object returnObject = joinPoint.proceed(args);
//            survivalClamProcessor.stop();
//            survivalThread.interrupt();
//            return returnObject;
//        }
        return result + "线程池: " + Thread.currentThread().getName()+" paymentInfo_LOCK, id: " + id;

    }

    @GetMapping("/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String result = paymentService.paymentInfo_OK(id);
        log.info("**********result" +result);
        return result;
    }


    @GetMapping("/error/{id}")
    public String paymentInfo_ERROR(@PathVariable("id") Integer id) {
        String result = paymentService.paymentInfo_ERROR(id);
        log.info("**********result" +result);
        return result;
    }

    @GetMapping("/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        String s = paymentService.paymentCircuitBreaker(id);
        log.info("***********result" + s);
        return s;
    }


}
